import store from './store'
import * as config from './config'
import utils from './utils'
import currency from './plugins/currency.js'
import router from './router'
import platform from './platform'
import defImg from '@/static/img/def-img.png'

// 加载App底层依赖
export async function AppInit() {
  const useApp = $.store('app')
  const useUser = $.store('user')
  const useShop = $.store('shop')
	
  useUser.$patch(state => {
	  state.isReady = state.isLogin ? Promise.resolve() : new Promise(resolve => state.onReady = resolve)
  })
  useShop.$patch(state => {
    state.isReady = state.shopId ? Promise.resolve() : new Promise(resolve => state.onReady = resolve)
  })
  // 应用初始化
  await Promise.all([useApp.init(), useUser.init()])
  await useShop.init()
}

import { reactive } from 'vue'

/**
* 数据 hook
* @param initValue 默认值
*/
export function useForm(initValue) {
  const form = reactive({ ...initValue })

  const resetForm = () => {
    Object.keys(form).forEach((key) => {
      form[key] = initValue ? initValue[key] : undefined
    })
  }

  const assignForm = (data) => {
    Object.keys(form).forEach((key) => {
      form[key] = data[key]
    })
  }

  return {
    form,
    // 重置为初始值
    resetForm,
    // 赋值不改变字段
    assignForm
  }
}

const $ = {
  currency,
  store,
  config,
  utils,
  get: utils.get,
  post: utils.post,
  useForm,
  router,
  platform,
  defImg
}

export default new Proxy($, {
  get(target, key) {
    return target[key] || uni.$u[key] || undefined
  }
})
